#include "BlockQueue.hpp"
#include "MyThread.hpp"
#include "Task.hpp"
#include <iostream>
#include <vector>
#include<unistd.h>
void producer(BlockQueue<Task>&bq)
{
    while(1)
    {
        sleep(1);
        bq.Enqueue(printhello);
    }

}
void consumer(BlockQueue<Task>&bq)
{
     while(1)
    {
        Task t;
        bq.Pop(&t);
        t();
    }
}
void startproducer(vector<MyThread::Thread<BlockQueue<Task>>>*threads, int num, BlockQueue<Task>&bq)
{
    for (int i = 1; i <= num; i++)
    {
        std::string name = "producer_" + std::to_string(i);
        MyThread::Thread<BlockQueue<Task>> t(producer,bq,name);
        threads->push_back(t);
    }
}
void startconsumer(vector<MyThread::Thread<BlockQueue<Task>>>*threads, int num, BlockQueue<Task>&bq)
{
    for (int i = 1; i <= num; i++)
    {
        std::string name = "consumer_" + std::to_string(i);
        MyThread::Thread<BlockQueue<Task>> t(consumer,bq,name);
        threads->push_back(t);
    }
}
void waitall(vector<MyThread::Thread<BlockQueue<Task>>>&threads)
{
    for (auto &thread : threads)
    {
        thread.join();
    }
}
int main()
{
    BlockQueue<Task> *pbq = new BlockQueue<Task>(5);
    vector<MyThread::Thread<BlockQueue<Task>>> threads;
    startproducer(&threads, 3, *pbq);
    startconsumer(&threads, 4, *pbq);
    for (auto &thread : threads)
    {
        thread.start();
    }
    waitall(threads);

    return 0;
}